package com.qingcloud.adminbackend.controller;


import com.qingcloud.adminbackend.anno.CloudApi;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.equipment.EquipmentDTO;
import com.qingcloud.adminbackend.entity.equipment.OrderEquipmentDTO;
import com.qingcloud.adminbackend.entity.sysorder.SysOrder;
import com.qingcloud.adminbackend.entity.sysorder.SysOrderBO;
import com.qingcloud.adminbackend.entity.sysorder.SysOrderPageDTO;
import com.qingcloud.adminbackend.entity.sysorder.SysUserOrderMgrDTO;
import com.qingcloud.adminbackend.entity.sysorg.SysUserModuleDTO;
import com.qingcloud.adminbackend.entity.user.SysUser;
import com.qingcloud.adminbackend.service.ISysOrderService;
import com.qingcloud.adminbackend.service.ISysUserService;
import com.qingcloud.adminbackend.utils.CookieUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author liyuhong
 * @since 2018-12-20
 */
@RestController
@RequestMapping("/sys-order")
public class SysOrderController {

    @Resource
    private ISysOrderService sysOrderService;
    @Resource
    private ISysUserService sysUserService;

    @ApiOperation(value = "查询订单列表Api", notes = "列表：\n" +
            "\t\t\n" +
            "\t\t完全管理权限和部分管理权限的客户，以及系统管理员会有不一样的列表数据\n" +
            "\t  \n" +
            "\t\t完全管理权限用户或系统管理员：\n" +
            "\t   \n" +
            "\t\t查询订单表，可编辑，可点操作按钮。\n" +
            "\t   \n" +
            "\t\t部分部分管理权限用户：\n" +
            "\t\t\n" +
            "\t\t通过sys_user_order_stationmgr 关联订单以及订单对应的客户表。\n" +
            "\n" +
            "\t  \n" +
            "\t\t订单内设备管理", response = SysOrder.class)
    @PostMapping("/findSysOrderList")
    public AjaxResult findSysOrderList(@RequestBody SysOrderPageDTO sysOrder, HttpServletRequest request) {
        String userId = CookieUtil.getUserId(request);
        return new AjaxResult(WamingConstant.SUCCESS, null, sysOrderService.findSysOrderList(userId, sysOrder));
    }

    @ApiOperation(value = "添加/修改订单Api", notes = "添加/修改订单Api", response = SysOrder.class)
    @PostMapping("/setSysOrder")
    public AjaxResult setSysOrder(@RequestBody SysOrder sysOrder, HttpServletRequest request) {
        sysOrder.setCreateBy(CookieUtil.getUserId(request));
        sysOrder.setModifyBy(CookieUtil.getUserId(request));
        return sysOrderService.setSysOrder(sysOrder);
    }

    @ApiOperation(value = "删除订单Api", notes = "删除订单Api")
    @PostMapping("/deleteOrder")
    public AjaxResult deleteOrder(@RequestBody List<String> orderIds) {
        return sysOrderService.deleteOrder(orderIds);
    }

    @ApiOperation(value = "订单关联客户Api", notes = "订单关联客户Api")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单Id", defaultValue = "1", required = false, dataType = "String", paramType =
            "query"), @ApiImplicitParam(name = "orgId", value = "客户Id", defaultValue = "1", required = false, dataType = "String", paramType =
            "query")})
    @GetMapping("/toOrderOrg")
    public AjaxResult toOrderOrg(String orderId, String orgId) {
        return sysOrderService.toOrderOrg(orderId, orgId);
    }

    @ApiOperation(value = "订单内设备列表Api", notes = "订单内设备列表Api", response = OrderEquipmentDTO.class)
    @PostMapping("/findOrderEquipmentList")
    public AjaxResult findOrderEquipmentList(@RequestBody EquipmentDTO equipmentDTO) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.findOrderEquipmentList(equipmentDTO));
    }

    @ApiOperation(value = "订单详情查看Api", notes = "进入页面时带出相关信息：\n" +
            "\t \n" +
            "\t\t1、客户下拉框\n" +
            "\t\t\n" +
            "\t\t\t系统管理员，完全管理权限的客服，带出所有以及客户； \n" +
            "\t\t\n" +
            "\t\t\t否则查询sys_user_org_mgr 中的一级客户，同时具有管理权限的所有客户）\n" +
            "\t\n" +
            "\t\t2、其他下拉框从数据字典查询；\n" +
            "\t\n" +
            "\t\t3、根据业务类型查询形影的功能类别和，模块，默认进入时带出第 选择的业务类型对应的功能类别（网格化）  \n" +
            "\t\t，功能列表默认勾选内容")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单Id", defaultValue = "1", required = false, dataType = "String", paramType =
            "query"),})
    @GetMapping("/findOrderById")
    public AjaxResult findOrderById(String orderId) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.findOrderById(orderId));
    }

    @CloudApi
    @ApiOperation(value = "根据订单ID查询订单信息", notes = "根据订单ID查询订单信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "订单Id", defaultValue = "1", required = false, dataType = "String", paramType =
            "query"),})
    @GetMapping("/detail")
    public AjaxResult findOrderDetail(String id) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.findOrderDetail(id));
    }

    @ApiOperation(value = "没有关联客户的订单Api", notes = "可选列表：订单表中未有客户，并且业务种类相同的，均可以作为可选列表；")
    @ApiImplicitParams({@ApiImplicitParam(name = "businessType", value = "业务类型（1：网格化，2:健康云）", defaultValue = "1", required = false, dataType =
            "String", paramType = "query"),})
    @GetMapping("/findOrderByBusinessType")
    public AjaxResult findOrderByBusinessType(String businessType) {
        Set<Integer> typeSet = Arrays.stream(businessType.split(",")).map(Integer::parseInt).collect(Collectors.toSet());
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.findOrderByBusinessType(typeSet));
    }

    @ApiOperation(value = "已关联客户的订单Api", notes = "已选列表：客户已经关联的订单")
    @ApiImplicitParams({@ApiImplicitParam(name = "orgId", value = "客户Id", defaultValue = "1", required = false, dataType = "String", paramType =
            "query")})
    @GetMapping("/findOrderByOrgId")
    public AjaxResult findOrderByOrgId(String orgId) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.findOrderByOrgId(orgId));
    }

    @ApiOperation(value = "客户批量关联订单Api", notes = "客户批量关联订单Api")
    @PostMapping("/orgToOrders")
    public AjaxResult orgToOrders(@RequestBody SysOrderBO sysOrderBO) {
        return sysOrderService.orgToOrders(sysOrderBO);
    }

    @ApiOperation(value = "用户授权【售前】和所属客户下的订单Api", notes = "用户授权【售前】和所属客户下的订单Api", response = SysUserOrderMgrDTO.class)
    @PostMapping("/getOrders")
    public AjaxResult getOrders(@RequestBody SysUserOrderMgrDTO sysUserOrderMgrDTO, HttpServletRequest request) {
        if (sysUserOrderMgrDTO.getBusinessTypes().size() == 0) {
            return new AjaxResult(WamingConstant.FAILURE, "请选择业务类型");
        }
        String userId = CookieUtil.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            return new AjaxResult(WamingConstant.FAILURE, "请重新登录");
        }
        boolean isAdmin = CookieUtil.isAdmin(request);
        SysUser user = sysUserService.getById(CookieUtil.getUserId(request));
        if (user.getAdminFlag() == 1) {
            isAdmin = true;
        }
        sysUserOrderMgrDTO.setUserId(userId);
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.getOrders(sysUserOrderMgrDTO, isAdmin));
    }

    @ApiOperation(value = "用户所属【售前】的订单Api", notes = "用户所属【售前】的订单Api", response = SysUserOrderMgrDTO.class)
    @PostMapping("/subordinateOrders")
    public AjaxResult subordinateOrders(@RequestBody SysUserOrderMgrDTO sysUserOrderMgrDTO) {
        if (sysUserOrderMgrDTO.getBusinessTypes().size() == 0) {
            return new AjaxResult(WamingConstant.FAILURE, "请选择业务类型");
        }
        if (StringUtils.isEmpty(sysUserOrderMgrDTO.getUserId())) {
            return new AjaxResult(WamingConstant.FAILURE, "请输入用户Id");
        }
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.grantedOrders(sysUserOrderMgrDTO));
    }

    @ApiOperation(value = "用户授权【运维】读写权限的订单Api", notes = "用户授权【运维】读写权限的订单Api", response = SysUserOrderMgrDTO.class)
    @PostMapping("/findOrders")
    public AjaxResult findOrders(@RequestBody SysUserOrderMgrDTO sysUserOrderMgrDTO, HttpServletRequest request) {
        if (sysUserOrderMgrDTO.getBusinessTypes().size() == 0) {
            return new AjaxResult(WamingConstant.FAILURE, "请选择业务类型");
        }
        String userId = CookieUtil.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            return new AjaxResult(WamingConstant.FAILURE, "请重新登录");
        }
        sysUserOrderMgrDTO.setUserId(userId);
        boolean isAdmin = CookieUtil.isAdmin(request);
        SysUser user = sysUserService.getById(CookieUtil.getUserId(request));
        if (user.getAdminFlag() == 1) {
            isAdmin = true;
        }
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.getOrders(sysUserOrderMgrDTO, isAdmin));
    }

    @ApiOperation(value = "用户已授权【运维】的订单Api", notes = "用户已授权【运维】的订单Api", response = SysUserOrderMgrDTO.class)
    @PostMapping("/grantedOrders")
    public AjaxResult grantedOrders(@RequestBody SysUserOrderMgrDTO sysUserOrderMgrDTO) {
        if (sysUserOrderMgrDTO.getBusinessTypes().size() == 0) {
            return new AjaxResult(WamingConstant.FAILURE, "请选择业务类型");
        }
        if (StringUtils.isEmpty(sysUserOrderMgrDTO.getUserId())) {
            return new AjaxResult(WamingConstant.FAILURE, "请输入用户Id");
        }
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.grantedOrders(sysUserOrderMgrDTO));
    }

    @ApiOperation(value = "用户已选【数据质控】的订单Api", notes = "用户【数据质控】的订单Api", response = SysUserOrderMgrDTO.class)
    @PostMapping("/dataQuality")
    public AjaxResult dataQuality(@RequestBody SysUserOrderMgrDTO sysUserOrderMgrDTO) {
        if (sysUserOrderMgrDTO.getBusinessTypes().size() == 0) {
            return new AjaxResult(WamingConstant.FAILURE, "请选择业务类型");
        }
        if (StringUtils.isEmpty(sysUserOrderMgrDTO.getUserId())) {
            return new AjaxResult(WamingConstant.FAILURE, "请输入用户Id");
        }
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.grantedOrders(sysUserOrderMgrDTO));
    }

    @ApiOperation(value = "客户及分支机构对应的订单Api", notes = "客户及分支机构对应的订单Api")
    @ApiImplicitParams({@ApiImplicitParam(name = "orgId", value = "客户Id", defaultValue = "1", required = false, dataType = "String", paramType =
            "query")})
    @GetMapping("/findOrderListByOrgId")
    public AjaxResult findOrderListByOrgId(String orgId) {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.findOrderListByOrgId(orgId));
    }

    @ApiOperation(value = "所有订单", notes = "所有订单", response = SysUserModuleDTO.class)
    @GetMapping("/findOrderList")
    public AjaxResult findOrderList() {
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.findOrderList());
    }

    @ApiOperation(value = "可管理的订单", notes = "可管理的订单", response = SysUserModuleDTO.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "businessType", value = "业务类型（1：网格化，2:健康云）", defaultValue = "1", required = false, dataType = "String", paramType =
            "query")})
    @GetMapping("/findOrders")
    public AjaxResult findOrders(String businessType, HttpServletRequest request) {
        String userId = CookieUtil.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            return new AjaxResult(WamingConstant.FAILURE, "请重新登录");
        }
        boolean isAdmin = CookieUtil.isAdmin(request);
        SysUser user = sysUserService.getById(CookieUtil.getUserId(request));
        if (user.getAdminFlag() == 1) {
            isAdmin = true;
        }
        return new AjaxResult(WamingConstant.SUCCESS, "", sysOrderService.findOrders(businessType, userId, isAdmin));
    }
}

